package ru.spb.osll.myfit.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import ru.spb.osll.myfit.client.dialog.ErrorDialog;
import ru.spb.osll.myfit.client.service.ChartComparisonService;
import ru.spb.osll.myfit.client.service.ChartComparisonServiceAsync;
import ru.spb.osll.myfit.common.chart.ChartData;

import java.util.ArrayList;
import java.util.List;

public class ChartComparisonViewController implements IViewController {
  private final ChartComparisonServiceAsync m_chartService = GWT.create(ChartComparisonService.class);
  private final ErrorDialog m_errorDialog = new ErrorDialog();
  private ChartComparisonView m_chartComparisonView = null;

  @Override
  public IView getView(String address) {
    final List<String> ids = new ArrayList<String>(Window.Location.getParameterMap().get("id"));

    m_chartComparisonView = new ChartComparisonView(ids, m_chartService);

    m_chartService.getChartDataList(ids, new AsyncCallback<List<ChartData>>() {
      @Override
      public void onFailure(Throwable caught) {
        m_errorDialog.setMessage(caught);
        m_errorDialog.center();
      }

      @Override
      public void onSuccess(List<ChartData> chartDataList) {
        m_chartComparisonView.display(chartDataList);
      }
    });
    return m_chartComparisonView;
  }
}
